/***************************************************************
 * Name:      RegionEditorMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    JackyLian (248023406@qq.com)
 * Created:   2012-12-20
 * Copyright: JackyLian ()
 * License:
 **************************************************************/

#include "RegionEditorMain.h"
#include <wx/msgdlg.h>
#include <wx/log.h>

//(*InternalHeaders(RegionEditorFrame)
#include <wx/artprov.h>
#include <wx/bitmap.h>
#include <wx/icon.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

#include "regionlistctrl.h"
#include "RegionEditDlg.h"

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(RegionEditorFrame)
const long RegionEditorFrame::ID_BUTTON1 = wxNewId();
const long RegionEditorFrame::ID_SEARCHCTRL1 = wxNewId();
const long RegionEditorFrame::ID_PANEL1 = wxNewId();
const long RegionEditorFrame::ID_LISTCTRL1 = wxNewId();
const long RegionEditorFrame::idMenuOpen = wxNewId();
const long RegionEditorFrame::idMenuSave = wxNewId();
const long RegionEditorFrame::idMenuQuit = wxNewId();
const long RegionEditorFrame::idMenuAbout = wxNewId();
const long RegionEditorFrame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(RegionEditorFrame,wxFrame)
    //(*EventTable(RegionEditorFrame)
    //*)
END_EVENT_TABLE()

RegionEditorFrame::RegionEditorFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(RegionEditorFrame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxBoxSizer* BoxSizer2;
    wxMenu* Menu1;
    wxBoxSizer* BoxSizer1;
    wxMenuBar* MenuBar1;
    wxMenu* Menu2;

    Create(parent, wxID_ANY, _("地区信息编辑器"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
    SetClientSize(wxSize(737,345));
    {
    	wxIcon FrameIcon;
    	FrameIcon.CopyFromBitmap(wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_NORMAL_FILE")),wxART_FRAME_ICON));
    	SetIcon(FrameIcon);
    }
    BoxSizer1 = new wxBoxSizer(wxVERTICAL);
    m_toolPanel = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
    m_newRegion = new wxButton(m_toolPanel, ID_BUTTON1, _("新建地区"), wxDefaultPosition, wxSize(67,29), 0, wxDefaultValidator, _T("ID_BUTTON1"));
    m_newRegion->Disable();
    BoxSizer2->Add(m_newRegion, 0, wxALL|wxSHAPED|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
    m_searchCtrl = new wxSearchCtrl(m_toolPanel, ID_SEARCHCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER, wxDefaultValidator, _T("ID_SEARCHCTRL1"));
    BoxSizer2->Add(m_searchCtrl, 4, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
    m_toolPanel->SetSizer(BoxSizer2);
    BoxSizer2->Fit(m_toolPanel);
    BoxSizer2->SetSizeHints(m_toolPanel);
    BoxSizer1->Add(m_toolPanel, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    m_regionVeiw = new RegionListCtrl(this, ID_LISTCTRL1, wxDefaultPosition, wxSize(815,307), wxLC_REPORT, wxDefaultValidator, _T("ID_LISTCTRL1"));
    BoxSizer1->Add(m_regionVeiw, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_TOP, 0);
    SetSizer(BoxSizer1);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem3 = new wxMenuItem(Menu1, idMenuOpen, _("打开\tCtrl+o"), wxEmptyString, wxITEM_NORMAL);
    Menu1->Append(MenuItem3);
    MenuItem4 = new wxMenuItem(Menu1, idMenuSave, _("另存为\tCtrl+s"), wxEmptyString, wxITEM_NORMAL);
    Menu1->Append(MenuItem4);
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("退出\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&文件"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("关于开发框架\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("帮助"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    m_fileDlg = new wxFileDialog(this, _("Select file"), wxEmptyString, wxEmptyString, wxFileSelectorDefaultWildcardStr, wxFD_DEFAULT_STYLE, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));
    m_saveDirDlg = new wxDirDialog(this, _("Select directory"), wxEmptyString, wxDD_DEFAULT_STYLE, wxDefaultPosition, wxDefaultSize, _T("wxDirDialog"));
    SetSizer(BoxSizer1);
    Layout();

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&RegionEditorFrame::Onm_newRegionClick);
    Connect(ID_SEARCHCTRL1,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&RegionEditorFrame::OnSearchCtrlSearchClicked);
    Connect(ID_SEARCHCTRL1,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&RegionEditorFrame::OnSearchCtrlSearchClicked);
    Connect(ID_SEARCHCTRL1,wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN,(wxObjectEventFunction)&RegionEditorFrame::OnSearchCtrlSearchClicked);
    Connect(ID_LISTCTRL1,wxEVT_COMMAND_LIST_ITEM_ACTIVATED,(wxObjectEventFunction)&RegionEditorFrame::OnRegionVeiwItemActivated);
    Connect(idMenuOpen,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&RegionEditorFrame::OnOpen);
    Connect(idMenuSave,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&RegionEditorFrame::OnSave);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&RegionEditorFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&RegionEditorFrame::OnAbout);
    Connect(wxEVT_SIZE,(wxObjectEventFunction)&RegionEditorFrame::OnResize);
    //*)
   // wxLog::SetActiveTarget(new wxLogWindow(this, wxT("Log")));
}

RegionEditorFrame::~RegionEditorFrame()
{
    //(*Destroy(RegionEditorFrame)
    //*)
}

void RegionEditorFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void RegionEditorFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void RegionEditorFrame::OnOpen(wxCommandEvent& event)
{
    if( m_fileDlg->ShowModal() == wxID_OK )
    {
        m_regionVeiw->Load(m_fileDlg->GetPath(), wxT(""));
        m_newRegion->Enable();
    }
}

void RegionEditorFrame::OnResize(wxSizeEvent& event)
{
    //m_regionVeiw->FitColumn();
    event.Skip();
}

void RegionEditorFrame::OnSave(wxCommandEvent& event)
{
    if( m_saveDirDlg->ShowModal() == wxID_OK )
    {
        m_regionVeiw->GetRegionManager().Save(m_saveDirDlg->GetPath());
    }
}

void RegionEditorFrame::OnRegionVeiwBeginLabelEdit(wxListEvent& event)
{
    wxLogMessage(wxT("%d %d has been edit!"), event.GetId(), event.GetColumn());
}

void RegionEditorFrame::OnRegionVeiwColumnClick(wxListEvent& event)
{
    wxLogMessage(wxT("%d %d has been edit!"), event.GetId(), event.GetColumn());
}

void RegionEditorFrame::OnRegionVeiwItemActivated(wxListEvent& event)
{
    if( m_regionVeiw->Edit(event.GetIndex()) )
    {
        m_regionVeiw->Reload(m_searchCtrl->GetValue());
    }
   // wxLogMessage(wxT("%d %d has been click!"), event.GetId(), event.GetColumn());
}

void RegionEditorFrame::OnSearchCtrlSearchClicked(wxCommandEvent& event)
{
    m_regionVeiw->Reload(m_searchCtrl->GetValue());
}

void RegionEditorFrame::Onm_newRegionClick(wxCommandEvent& event)
{
    if( m_regionVeiw->CreateRegion() )
    {
        m_regionVeiw->Reload(m_searchCtrl->GetValue());
    }
}
